<?php
/**
 * Created by xiaosong
 * E-mail:4155433@gmail.com
 * Date: 2019/7/26
 * Time: 10:49
 */

namespace app\common\service;
use app\api\controller\Base;
use app\common\model\ChatUsers;
use app\common\model\Room;
use Monolog\Handler\DynamoDbHandler;
use think\Db;

class AppService
{
    /**
     * 获取用户信息
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public static function getUserInfo($field = '',$user_id,$cache = 2){

        if (!is_numeric($user_id)){
            $map['a.user_id'] = dehashid($user_id);
        }else {
            $map['a.user_id'] = $user_id;
        }

        if (!is_numeric($map['a.user_id'])){
            return null;
        }

        if (!$cache){
            $data =  Db::name('users')->alias('a')->join('user_extend e','e.user_id = a.user_id','left')->where($map)->cache($cache)->find();
        }else{
            $data =  Db::name('users')->alias('a')->join('user_extend e','e.user_id = a.user_id','left')->where($map)->find();
        }

        if (strstr($field,',') || empty($field)){
            if (!$field){
                $arr = array_keys($data);
            }else{
                $arr = explode(',',$field);
            }
            $array = [];
            foreach ($arr as $v){
                $array[$v] = self::userHandle($v,$data[$v],$data);
            }
            return $array;
        }else{
            return self::userHandle($field,$data[$field],$data);
        }

    }

    /**
     * 用户信息预处理
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public static function userHandle(string $field,$value,$data = [])
    {

        switch ($field){
            case 'noble_id':
                $value = Base::checkNoble($data);
                break;
            default:
                break;
        }

        return $value;
    }

    public static function getUserRoomId(int $user_id,bool $fail = false)
    {
        $map['user_id'] = $user_id;
        $id = Room::where($map)->cache(15)->value('room_id');
        if ($fail){
            if (!is_numeric($id)){
                api_return(0,'您还没有房间!');
            }
        }
        return $id;
    }

    public static function getUserGroupId(int $user_id):int
    {
        $where['user_id'] = $user_id;
        $where['status'] = 1;

        return ChatUsers::where($where)->value('group_id')??0;
    }

    /**
     * 根据权重返回房间id
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public static function getRandRoom()
    {
        $map = [];
        $map['weight'] = ['>',0];
        $map['status'] = 1;

        $list = Room::where($map)->field('weight,room_id')->cache(5)->select();

        $items = [];

        foreach ($list as $k => $v){
            for ($i = 0;$i <= $v['weight'];$i++){
                $items[] = $v['room_id'];
            }
        }

        shuffle($items);
        return $items[0]??0;
    }


    /**
     * 查询设备是否被封禁
     * @param bool $fail
     * @return bool
     */
    public static function checkBand($fail = true)
    {
        //判断设备或ip是否被封禁
        $ip = \request()->ip();

        $id = Db::name('band')->where('IMEI',$ip)->cache(3)->value('id');
        if ($id && $fail){
            api_return(0,'IP已被封禁');
        }

        $IMEI = input('IMEI');
        if (!empty($IMEI)){
            //判断设备号是否被封禁
            $id = Db::name('band')->where('IMEI',$IMEI)->cache(3)->value('id');
            if ($id && $fail){
                api_return(0,'设备已被封禁');
            }
        }

        return true;

    }

    /**
     * 朝全局变量存入参数
     */
    public static function pushGlobal(string $golbar,array $params)
    {
        global $$golbar;
        return $$golbar = is_array($$golbar) ? array_merge($$golbar,$params) : $params;

    }


    /**
     * 判断是否为会员
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public static function isMember($user_id):bool
    {
        if (!isInt($user_id)){
            return false;
        }
        $member_time = self::getUserInfo('member_time',$user_id);
        if ($member_time >= time()){
            return true;
        }
        return false;

    }


}